﻿using System;
using com.bodurov.Html.HtmlSummarizer;
using NUnit.Framework;

namespace com.bodurov.Html.HtmlSummarizerTests
{
    /*
    Blog: http://blog.bodurov.com/Html-Summarizer
    */
    [TestFixture]
    public class HtmlSummarizerTests
    {
        private static readonly ISummarizer _summarizer = new Summarizer();

        [Test]
        public void HtmlSummarizer_CanGetHtmlSummary_ByLetters()
        {
            const string html = @"
                <div>
                    <h1 align=""center"">The title goes here.</h1>
                    <img src=""/images/i.gif"">
                    <div>
                        <span style='color:red'>T</span>he text starts here.
                    </div>
                    <p>
                        Then a paragraph follows.
                    </p>
                    <p>
                        And another one afterwords.
                    </p>
                </div>
            ";

            var summary = _summarizer.GetHtmlSummary(html, 50, PartType.Letter);

            //Console.WriteLine(summary);

            Assert.AreEqual(@"<div>
                    <h1 align=""center"">The title goes here.</h1>
                    <img src=""/images/i.gif"" />
                    <div>
                        <span style='color:red'>T</span>he text starts here.
                    </div>
                    <p>
                        Then a paragraph f</p></div>", summary);



        }

        [Test]
        public void HtmlSummarizer_CanGetHtmlSummary_ByWord()
        {
            const string html = @"
                <div>
                    <h1 align=""center"">The title goes here.</h1>
                    <img src=""/images/i.gif"">
                    <div>
                        <span style='color:red'>T</span>he text starts here.
                    </div>
                    <p>
                        Then a paragraph follows.
                    </p>
                    <p>
                        And another one afterwords.
                    </p>
                </div>
            ";

            var summary = _summarizer.GetHtmlSummary(html, 7, PartType.Word);

            //Console.WriteLine(summary);

            Assert.AreEqual(@"<div>
                    <h1 align=""center"">The title goes here.</h1>
                    <img src=""/images/i.gif"" />
                    <div>
                        <span style='color:red'>T</span>he text starts </div></div>", summary);

        }

        [Test]
        public void HtmlSummarizer_CanGetHtmlSummary_BySentance()
        {
            const string html = @"
                <div>
                    <h1 align=""center"">The title goes here.</h1>
                    <hr width=""100%"">
                    <div>
                        <span style='color:red'>T</span>he text starts here.
                    </div>
                    <p>
                        Then a paragraph follows.
                    </p>
                    <p>
                        And another one afterwords.
                    </p>
                </div>
            ";

            var summary = _summarizer.GetHtmlSummary(html, 3, PartType.Sentance);

            //Console.WriteLine(summary);

            Assert.AreEqual(@"<div>
                    <h1 align=""center"">The title goes here.</h1>
                    <hr width=""100%"" />
                    <div>
                        <span style='color:red'>T</span>he text starts here.
                    </div>
                    <p>
                        Then a paragraph follows.</p></div>", summary);

        }

        [Test]
        public void HtmlSummarizer_CanGetHtmlSummary_ByClosedHtmlTag()
        {
            const string html = @"
                <div>
                    <h1 align=""center"">The title goes here.</h1>
                    <hr width=""100%"">
                    <div>
                        <span style='color:red'>T</span>he text starts here.
                    </div>
                    <p>
                        Then a paragraph follows.
                    </p>
                    <p>
                        And another one afterwords.
                    </p>
                </div>
            ";

            var summary = _summarizer.GetHtmlSummary(html, 5, PartType.ClosedHtmlTag);

            //Console.WriteLine(summary);

            Assert.AreEqual(@"<div>
                    <h1 align=""center"">The title goes here.</h1>
                    <hr width=""100%"" />
                    <div>
                        <span style='color:red'>T</span>he text starts here.
                    </div>
                    <p>
                        Then a paragraph follows.
                    </p></div>", summary);

        }

        [Test]
        public void HtmlSummarizer_CanGetHtmlSummary_ByClosedDivTag()
        {
            const string html = @"
                <div>
                    <h1 align=""center"">The title goes here.</h1>
                    <hr />
                    <div>
                        <span style='color:red'>T</span>he text starts here.
                    </div>
                    <p>
                        Then a paragraph follows.
                    </p>
                    <div align=""right"">And here goes my seccond div tag</div>
                    <p>
                        And another one afterwords.
                    </p>
                </div>
            ";

            var summary = _summarizer.GetHtmlSummary(html, 2, PartType.ClosedDivTag);

            //Console.WriteLine(summary);

            Assert.AreEqual(@"<div>
                    <h1 align=""center"">The title goes here.</h1>
                    <hr />
                    <div>
                        <span style='color:red'>T</span>he text starts here.
                    </div>
                    <p>
                        Then a paragraph follows.
                    </p>
                    <div align=""right"">And here goes my seccond div tag</div></div>", summary);

        }

        [Test]
        public void HtmlSummarizer_CanGetHtmlSummary_ByClosedPTag()
        {
            const string html = @"
                <div>
                    <h1 align=""center"">The title goes here.</h1>
                    <hr />
                    <div>
                        <span style='color:red'>T</span>he text starts here.
                    </div>
                    <p>
                        Then a paragraph follows.
                    </p>
                    <p>And here goes my seccond p tag</p>
                    <p>
                        And another one afterwords.
                    </p>
                </div>
            ";

            var summary = _summarizer.GetHtmlSummary(html, 2, PartType.ClosedPTag);

            //Console.WriteLine(summary);

            Assert.AreEqual(@"<div>
                    <h1 align=""center"">The title goes here.</h1>
                    <hr />
                    <div>
                        <span style='color:red'>T</span>he text starts here.
                    </div>
                    <p>
                        Then a paragraph follows.
                    </p>
                    <p>And here goes my seccond p tag</p></div>", summary);

        }

        [Test]
        public void HtmlSummarizer_CanGetHtmlSummary_ByClosedDivTagOrPTag()
        {
            const string html = @"
                <div>
                    <h1 align=""center"">The title goes here.</h1>
                    <hr />
                    <div>
                        <span style='color:red'>T</span>he text starts here.
                    </div>
                    <p>
                        Then a paragraph follows.
                    </p>
                    <p>And here goes my seccond p tag</p>
                    <p>
                        And another one afterwords.
                    </p>
                </div>
            ";

            var summary = _summarizer.GetHtmlSummary(html, 2, PartType.ClosedDivTagOrPTag);

            //Console.WriteLine(summary);

            Assert.AreEqual(@"<div>
                    <h1 align=""center"">The title goes here.</h1>
                    <hr />
                    <div>
                        <span style='color:red'>T</span>he text starts here.
                    </div>
                    <p>
                        Then a paragraph follows.
                    </p></div>", summary);

        }



        [Test]
        public void HtmlSummarizer_CanGetHtmlSummary_ByWordsAfterBodyTag()
        {
            const string html = @"
                <html>
                    <head>
                        <title>Title</title>
                    </head>
                    <Body>
                        <div>
                            <h1 align=""center"">The title goes here.</h1>
                            <img src=""/images/i.gif"">
                            <div>
                                <span style='color:red'>T</span>he text starts here.
                            </div>
                            <p>
                                Then a paragraph follows.
                            </p>
                            <p>
                                And another one afterwords.
                            </p>
                        </div>
                    </Body>
                </html>
            ";

            var summary = 
                _summarizer.GetHtmlSummary( html, 
                                            s => s.IndexOf(">", s.IndexOf("<body", StringComparison.CurrentCultureIgnoreCase) + 1) + 1, 
                                            7, 
                                            PartType.Word);

            Console.WriteLine(summary);

            Assert.AreEqual(@"<div>
                            <h1 align=""center"">The title goes here.</h1>
                            <img src=""/images/i.gif"" />
                            <div>
                                <span style='color:red'>T</span>he text starts </div></div>", summary);



        }

    }
}
